﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.Diagnostics;

using Microsoft.Xna.Framework.Graphics;


namespace DarwinIsDead.Traits {
    public class SlowHeal : Trait {

        public float healthMax;
        public float tempHealth;
        private Texture2D mySprite;
        bool healed;
        byte oldR;
        byte oldB;
        private SpriteFont font;
        int greenIterations;
        float rateOfGrowth;
        private Color COLOREDPEOPLE;
        private const int FADE_STEPS = 60;

        public SlowHeal(Zombie owner, World world = null)
         : base(owner, world) {
        }

        public override void Initialize() {
            Owner.Color.A = 255;  // set initial transparency to max.
            tempHealth = 255;
            //healthMax = 40;
            healed = false;
            //oldR = Owner.Color.R;
            //oldB = Owner.Color.B;
            font = World.Game.Content.Load<SpriteFont>("Fonts\\Courier New");
            greenIterations = FADE_STEPS;
            mySprite = World.Game.Content.Load<Texture2D>("Sprites\\Traits\\SlowHeal");
            Owner.HitBox.Height += 10;
            Owner.HitBox.Width += 10;
            Owner.MaxHealth += 4;
            //COLOREDPEOPLE = Color.Transparent;
            COLOREDPEOPLE.A = 0;
            COLOREDPEOPLE.R = 0;
            COLOREDPEOPLE.G = 0;
            COLOREDPEOPLE.B = 0;
        }

        protected override void AdjustLevel(int level) {
            rateOfGrowth = 10.0f * level;
            Debug.WriteLine(rateOfGrowth);
        }

        protected override void InternalDraw(SpriteBatch spriteBatch, Color color) {
            Vector2 drawPosition = new Vector2(Owner.Position.X + 50, Owner.Position.Y);

            //spriteBatch.DrawString(font, "Health " + Owner.Health, drawPosition, color);
            spriteBatch.Draw(mySprite, drawPosition, COLOREDPEOPLE);
        }

        protected override void InternalUpdate(GameTime gameTime) {

            float dt = (float)((float)gameTime.ElapsedGameTime.Milliseconds / 1000f);

            if (Owner.Health < Owner.MaxHealth) {
                tempHealth += dt * rateOfGrowth;
            }

            int old = Owner.Health;
            Owner.Health += (byte)tempHealth;

            if (old != Owner.Health)
                greenIterations = 0;

            if (greenIterations < FADE_STEPS) {
                /*Owner.Color.B = 0;
                Owner.Color.R = 0;*/
                COLOREDPEOPLE.A = (byte)((float)greenIterations / (float)FADE_STEPS);
                COLOREDPEOPLE.R = (byte)((float)greenIterations / (float)FADE_STEPS);
                COLOREDPEOPLE.G = (byte)((float)greenIterations / (float)FADE_STEPS);
                COLOREDPEOPLE.B = (byte)((float)greenIterations / (float)FADE_STEPS);
                tempHealth = 0;
                greenIterations++;
                //Owner.Color.G = oldG;
                //spriteBatch.Draw(sprite, drawPositon, null, Color.Red, rotateMe, Vector2.Zero, scaleMe, SpriteEffects.None, 0);
            }
            else if (greenIterations == FADE_STEPS) {
                /*Owner.Color.B = oldB;
                Owner.Color.R = oldR;*/
                COLOREDPEOPLE.A = 255;
                COLOREDPEOPLE = Color.White;
                //Owner.Color.G = oldG;
                // spriteBatch.Draw(sprite, drawPositon, null, Color.Red, rotateMe, Vector2.Zero, scaleMe, SpriteEffects.None, 0);
            }
        
        }

        public override object Clone() {
            return new SlowHeal(Owner, World);
        }

    }
}
